﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using StudentManage.Data;
using StudentManage.Common;

namespace StudentManage.Web.Admin
{
    public partial class Login : SecurityPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                string temuname = UtilityUI.GetCookie("SPZCFX_UName");
                string temppass = UtilityUI.GetCookie("SPZCFX_UPass");
                if (!string.IsNullOrEmpty(temppass) && !string.IsNullOrEmpty(temuname))
                {
                    temppass = UtilityUI.Decode(temppass);
                    this.txt_UserName.Text = temuname;
                    this.txt_PassWord.Attributes["value"] = temppass;
                    this.CK_SavePass.Checked = true;
                }
            }
        }

        protected void Btn_login_Click(object sender, EventArgs e)
        {
            string userName = txt_UserName.Text.Trim();
            string passWord = txt_PassWord.Text.Trim();

            UserList Umd=UserList.Where("UserName=@0 and PassWord=@1", userName, Utility.GetMD5PassWord(passWord, "")).FirstOrDefault();
            xy_xsxx xsmd = null;
            xy_jgxx jgmd = null;
            if (Umd != null)
            {
                if (Convert.ToBoolean(Umd.IsStudent))
                {
                    xsmd = xy_xsxx.Where("XH=@0", Umd.XH).FirstOrDefault();
                }
                else
                {
                    jgmd = xy_jgxx.Where("ZGH=@0", Umd.XH).FirstOrDefault();
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('用户名或密码错误!');</script>");
                return;
            }


            if (xsmd!=null||jgmd!=null)
            {
                FormsAuthentication.SetAuthCookie(userName, false);
                if (this.CK_SavePass.Checked)
                {
                    UtilityUI.WriteCookie("SPZCFX_UName", userName, 24 * 60);
                    UtilityUI.WriteCookie("SPZCFX_UPass", UtilityUI.Encode(passWord), 24 * 60);
                }
                else
                {
                    UtilityUI.WriteCookie("SPZCFX_UName", userName, -1);
                    UtilityUI.WriteCookie("SPZCFX_UPass", UtilityUI.Encode(passWord), -1);
                }
                Response.Redirect("Main.aspx");
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('数据信息不完整!');</script>");
            }
        }
    }
}